package com.xumingxun.aibox.domain.condition;

import com.xumingxun.aibox.context.Context;
import com.xumingxun.aibox.domain.node.Node;
import com.xumingxun.aibox.enums.BoolEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@Accessors(chain = true)
public class Branch {
    String name;
    List<Compare> compares;
    BoolEnum bool=BoolEnum.AND;
    Node node;

    public boolean execute(Context context) {
        if(bool==BoolEnum.AND){
            for (Compare compare : compares) {
                if (!compare.execute(context)) {
                    return false;
                }
            }
            return true;
        }else if(bool==BoolEnum.OR){
            for (Compare compare : compares) {
                if (compare.execute(context)) {
                    return true;
                }
            }
            return false;
        }else{
            throw new RuntimeException("Branch bool type error, only AND and OR are supported");
        }
    }
}
